package com.autonavi.amapauto;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tonyzxb.autoemulator.MainFragment;
import com.example.tonyzxb.autoemulator.R;
import com.example.tonyzxb.autoemulator.model.IntentBean;

import java.util.ArrayList;

/**
 * 显示悬浮框 服务
 */
public class ClientService extends Service {

    public static final String TAG="AUTO Emulator Send";
    private static final double LAT = 118.13220323;
    private static final double LNG = 24.52141512;
    boolean isNiht = true;
    int i = 0;

    private WindowManager wm=null;
    private WindowManager.LayoutParams wmParams=null;
    private MyFloatView myFV=null;
    private ArrayList<IntentBean.SendEntity> intentList;
    private ClientService clientService;

    @Override
    public void onCreate() {
        super.onCreate();
        clientService = this;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
      intentList = (ArrayList<IntentBean.SendEntity>)intent.getSerializableExtra(Intent.EXTRA_TEXT);
        createView();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createView(){
        myFV=new MyFloatView(getApplicationContext());
        //获取WindowManager
        wm=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        //设置LayoutParams(全局变量）相关参数
        wmParams = ((MyApplication)getApplication()).getMywmParams();

        /**
         *以下都是WindowManager.LayoutParams的相关属性
         * 具体用途可参考SDK文档
         */
        wmParams.type= WindowManager.LayoutParams.TYPE_PHONE;   //设置window type
        wmParams.format=PixelFormat.RGBA_8888;   //设置图片格式，效果为背景透明

        //设置Window flag
        wmParams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        /*
         * 下面的flags属性的效果形同“锁定”。
         * 悬浮窗不可触摸，不接受任何事件,同时不影响后面的事件响应。
         wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
                               | LayoutParams.FLAG_NOT_FOCUSABLE
                               | LayoutParams.FLAG_NOT_TOUCHABLE;
        */
        wmParams.gravity=Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角
        //以屏幕左上角为原点，设置x、y初始值
        wmParams.x=0;
        wmParams.y=0;

        //设置悬浮窗口长宽数据
        wmParams.width=dip2px(this,200);
        wmParams.height=dip2px(this,300);

        LayoutInflater inflater = LayoutInflater.from(getApplication());
        //获取浮动窗口视图所在布局
        LinearLayout mFloatLayout = (LinearLayout) inflater.inflate(R.layout.client_service, null);
        Button btnExit = (Button) mFloatLayout.findViewById(R.id.btn_exit);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopSelf();
            }
        });
        LinearLayout  scrollViewLinearLayout = (LinearLayout)mFloatLayout.findViewById(R.id.scrollview_line);

        for (final IntentBean.SendEntity sendEntity : intentList) {
            TextView textView = new TextView(getApplicationContext());
            textView.setHeight(dip2px(this,40));
            textView.setTag(sendEntity.name);
            textView.setText(sendEntity.name);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.WHITE);
            textView.setBackgroundResource(R.drawable.btn_background);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setAction(sendEntity.action);
                    MainFragment.putExtra(intent, sendEntity.extra);
                    clientService.sendBroadcast(intent);
                    String msg = MainFragment.createMsg(intent);
                    Log.e(TAG,msg);
                    Toast.makeText(clientService, msg, Toast.LENGTH_SHORT).show();
                }
            });
            scrollViewLinearLayout.addView(textView);
        }
        myFV.addView(mFloatLayout);
        //显示myFloatView图像
        wm.addView(myFV, wmParams);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (mFloatLayout != null) {
//            //移除悬浮窗口
//            mWindowManager.removeView(mFloatLayout);
//        }
        wm.removeView(myFV);
    }


    public static int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }

}
